The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
CHANGES 06
META.yml 718
README 425
lib/HTML/TagCloud.pm 1271
t/simple.t 178
5 files changed (This is a version diff) 24198
@@ -1,5 +1,11 @@
 CHANGES file for HTML::TagCloud:
 
+0.36 Wed Feb 16 15:10:49 MST 2011
+  - use different styles to distinguish adjacent tags.
+
+0.35 Wed Feb 16 10:27:55 MST 2011
+  - make tag URLs optional.
+
 0.34 Tue Nov  7 21:00:33 GMT 2006
   - Internet Explorer fix, which addresses issues with Japanese text
     (thanks to Tatsuhiko Miyagawa)
@@ -1,8 +1,19 @@
 --- #YAML:1.0
-name: HTML-TagCloud
-version: 0.34
-author:
-  - Leon Brocard, C<< <acme@astray.com> >>.
-abstract: Generate An HTML Tag Cloud
-license: perl
-generated_by: Module::Build version 0.2612, without YAML.pm
+name:               HTML-TagCloud
+version:            0.36
+abstract:           ~
+author:  []
+license:            unknown
+distribution_type:  module
+configure_requires:
+    ExtUtils::MakeMaker:  0
+requires:
+    Test::More:  0
+no_index:
+    directory:
+        - t
+        - inc
+generated_by:       ExtUtils::MakeMaker version 6.48
+meta-spec:
+    url:      http://module-build.sourceforge.net/META-spec-v1.4.html
+    version:  1.4
@@ -2,11 +2,18 @@ NAME
     HTML::TagCloud - Generate An HTML Tag Cloud
 
 SYNOPSIS
+      # A cloud with tags that link to other web pages.
       my $cloud = HTML::TagCloud->new;
       $cloud->add($tag1, $url1, $count1);
       $cloud->add($tag2, $url2, $count2);
       $cloud->add($tag3, $url3, $count3);
       my $html = $cloud->html_and_css(50);
+
+      # A cloud with tags that do not link to other web pages.
+      my $cloud = HTML::TagCloud->new;
+      $cloud->add_static($tag1, $count1);
+      $cloud->add_static($tag2, $count2);
+      $cloud->add_static($tag3, $count3);
   
 DESCRIPTION
     The HTML::TagCloud module enables you to generate "tag clouds" in HTML.
@@ -25,11 +32,18 @@ DESCRIPTION
 
 CONSTRUCTOR
   new
-    The constructor takes one optional argument:
+    The constructor takes two optional arguments:
 
       my $cloud = HTML::TagCloud->new(levels=>10);
 
-    if not provided, leves defaults to 24
+    if not provided, levels defaults to 24
+
+      my $cloud = HTML::TagCloud->new(distinguish_adjacent_tags=>1);
+
+    If distinguish_adjacent_tags is true HTML::TagCloud will use different
+    CSS classes for adjacent tags in order to be able to make it easier to
+    distinguish adjacent multi-word tags. If not specified, this parameter
+    defaults to a false value.
 
 METHODS
   add
@@ -40,9 +54,16 @@ METHODS
       $cloud->add($tag2, $url2, $count2);
       $cloud->add($tag3, $url3, $count3);
 
+  add_static
+    This module adds a tag that does not link to another web page into the
+    cloud. You pass in the tag name and its count:
+
+      $cloud->add_static($tag1, $count1);
+      $cloud->add_static($tag2, $count2);
+
   tags($limit)
     Returns a list of hashrefs representing each tag in the cloud, sorted by
-    alphabet. each tag has the following keys; name, count, url and level.
+    alphabet. Each tag has the following keys: name, count, url and level.
 
   css
     This returns the CSS that will format the HTML returned by the html()
@@ -69,7 +90,7 @@ AUTHOR
     Leon Brocard, "<acme@astray.com>".
 
 COPYRIGHT
-    Copyright (C) 2005, Leon Brocard
+    Copyright (C) 2005-6, Leon Brocard
 
     This module is free software; you can redistribute it or modify it under
     the same terms as Perl itself.
@@ -1,14 +1,15 @@
 package HTML::TagCloud;
 use strict;
 use warnings;
-our $VERSION = '0.34';
+our $VERSION = '0.36';
 
 sub new {
   my $class = shift;
   my $self  = {
-    counts => {},
-    urls   => {},
-    levels => 24,
+    counts                    => {},
+    urls                      => {},
+    levels                    => 24,
+    distinguish_adjacent_tags => 0,
     @_
   };
   bless $self, $class;
@@ -21,6 +22,11 @@ sub add {
   $self->{urls}->{$tag} = $url;
 }
 
+sub add_static {
+    my ($self, $tag, $count) = @_;
+    $self->{counts}->{$tag} = $count;
+}
+
 sub css {
   my ($self) = @_;
   my $css = q(
@@ -30,13 +36,25 @@ sub css {
 }
 );
   foreach my $level (0 .. $self->{levels}) {
-    my $font = 12 + $level;
-    $css .= "span.tagcloud$level { font-size: ${font}px;}\n";
-    $css .= "span.tagcloud$level a {text-decoration: none;}\n";
+    if ( $self->{distinguish_adjacent_tags} ) {
+      $css .= $self->_css_for_tag($level, 'even');
+      $css .= $self->_css_for_tag($level, 'odd');
+    } else {
+      $css .= $self->_css_for_tag($level, q{});
+    }
   }
   return $css;
 }
 
+sub _css_for_tag {
+    my ($self, $level, $subclass) = @_;
+    my $font = 12 + $level;
+    return <<"END_OF_TAG";
+span.tagcloud${level}${subclass} {font-size: ${font}px;}
+span.tagcloud${level}${subclass} a {text-decoration: none;}
+END_OF_TAG
+}
+
 sub tags {
   my($self, $limit) = @_;
   my $counts = $self->{counts};
@@ -82,8 +100,8 @@ sub html {
     return "";
   } elsif ($ntags == 1) {
     my $tag = $tags[0];
-    return qq{<div id="htmltagcloud"><span class="tagcloud1"><a href="}.
-	$tag->{url}.qq{">}.$tag->{name}.qq{</a></span></div>\n};
+    my $span = $self->_format_span(@{$tag}{qw(name url)}, 1, 1);
+    return qq{<div id="htmltagcloud">$span</div>\n};
   }
 
 #  warn "min $min - max $max ($factor)";
@@ -91,9 +109,11 @@ sub html {
 #  warn(($max - $min) * $factor);
 
   my $html = "";
+  my $is_even = 1;
   foreach my $tag (@tags) {
-    $html .=  qq{<span class="tagcloud}.$tag->{level}.qq{"><a href="}.$tag->{url}.
-	      qq{">}.$tag->{name}.qq{</a></span>\n};
+    my $span = $self->_format_span(@{$tag}{qw(name url level)}, $is_even);
+    $html .= "$span\n";
+    $is_even = !$is_even;
   }
   $html = qq{<div id="htmltagcloud">
 $html</div>};
@@ -107,6 +127,24 @@ sub html_and_css {
   return $html;
 }
 
+sub _format_span {
+  my ($self, $name, $url, $level, $is_even) = @_;
+  my $subclass = q{};
+  if ( $self->{distinguish_adjacent_tags} ) {
+      $subclass = $is_even ? 'even' : 'odd';
+  }
+  my $span_class = qq{tagcloud$level$subclass};
+  my $span = qq{<span class="$span_class">};
+  if (defined $url) {
+    $span .= qq{<a href="$url">};
+  }
+  $span .= $name;
+  if (defined $url) {
+    $span .= qq{</a>};
+  }
+  $span .= qq{</span>};
+}
+
 1;
 
 __END__
@@ -117,11 +155,18 @@ HTML::TagCloud - Generate An HTML Tag Cloud
 
 =head1 SYNOPSIS
 
+  # A cloud with tags that link to other web pages.
   my $cloud = HTML::TagCloud->new;
   $cloud->add($tag1, $url1, $count1);
   $cloud->add($tag2, $url2, $count2);
   $cloud->add($tag3, $url3, $count3);
   my $html = $cloud->html_and_css(50);
+
+  # A cloud with tags that do not link to other web pages.
+  my $cloud = HTML::TagCloud->new;
+  $cloud->add_static($tag1, $count1);
+  $cloud->add_static($tag2, $count2);
+  $cloud->add_static($tag3, $count3);
   
 =head1 DESCRIPTION
 
@@ -143,12 +188,19 @@ or use your own.
 
 =head2 new
 
-The constructor takes one optional argument:
+The constructor takes two optional arguments:
 
   my $cloud = HTML::TagCloud->new(levels=>10);
 
 if not provided, levels defaults to 24
 
+  my $cloud = HTML::TagCloud->new(distinguish_adjacent_tags=>1);
+
+If distinguish_adjacent_tags is true HTML::TagCloud will use different CSS
+classes for adjacent tags in order to be able to make it easier to
+distinguish adjacent multi-word tags.  If not specified, this parameter
+defaults to a false value.
+
 =head1 METHODS
 
 =head2 add
@@ -160,6 +212,13 @@ and its count:
   $cloud->add($tag2, $url2, $count2);
   $cloud->add($tag3, $url3, $count3);
 
+=head2 add_static
+
+This module adds a tag that does not link to another web page into the
+cloud.  You pass in the tag name and its count:
+
+  $cloud->add_static($tag1, $count1);
+  $cloud->add_static($tag2, $count2);
 
 =head2 tags($limit)
 
@@ -1,6 +1,6 @@
 #!perl
 use strict;
-use Test::More tests => 12;
+use Test::More tests => 20;
 use_ok('HTML::TagCloud');
 
 my $cloud = HTML::TagCloud->new;
@@ -60,6 +60,83 @@ is($html, q{<div id="htmltagcloud">
 <span class="tagcloud3"><a href="c.html">c</a></span>
 </div>});
 
+$cloud = HTML::TagCloud->new( distinguish_adjacent_tags => 1 );
+$cloud->add("a", "a.html", 10);
+$cloud->add("b", "b.html", 10);
+$cloud->add("c", "c.html", 10);
+
+$css = $cloud->css;
+is(lines($css), 105);
+
+$html = $cloud->html();
+is($html, q{<div id="htmltagcloud">
+<span class="tagcloud3even"><a href="a.html">a</a></span>
+<span class="tagcloud3odd"><a href="b.html">b</a></span>
+<span class="tagcloud3even"><a href="c.html">c</a></span>
+</div>});
+
+$cloud = HTML::TagCloud->new;
+$cloud->add_static("a", 10);
+
+$html = $cloud->html();
+is ($html, q{<div id="htmltagcloud"><span class="tagcloud1">a</span></div>
+});
+
+$cloud = HTML::TagCloud->new( distinguish_adjacent_tags => 1 );
+$cloud->add_static("a", 10);
+
+$html = $cloud->html();
+is ($html, q{<div id="htmltagcloud"><span class="tagcloud1even">a</span></div>
+});
+
+$cloud = HTML::TagCloud->new;
+$cloud->add_static("a", 10);
+$cloud->add_static("b", 10);
+$cloud->add_static("c", 10);
+
+$html = $cloud->html();
+is($html, q{<div id="htmltagcloud">
+<span class="tagcloud3">a</span>
+<span class="tagcloud3">b</span>
+<span class="tagcloud3">c</span>
+</div>});
+
+$cloud = HTML::TagCloud->new( distinguish_adjacent_tags => 1 );
+$cloud->add_static("a", 10);
+$cloud->add_static("b", 10);
+$cloud->add_static("c", 10);
+
+$html = $cloud->html();
+is($html, q{<div id="htmltagcloud">
+<span class="tagcloud3even">a</span>
+<span class="tagcloud3odd">b</span>
+<span class="tagcloud3even">c</span>
+</div>});
+
+$cloud = HTML::TagCloud->new;
+$cloud->add("a", "a.html", 10);
+$cloud->add_static("b", 10);
+$cloud->add("c", "c.html", 10);
+
+$html = $cloud->html();
+is($html, q{<div id="htmltagcloud">
+<span class="tagcloud3"><a href="a.html">a</a></span>
+<span class="tagcloud3">b</span>
+<span class="tagcloud3"><a href="c.html">c</a></span>
+</div>});
+
+$cloud = HTML::TagCloud->new( distinguish_adjacent_tags => 1 );
+$cloud->add("a", "a.html", 10);
+$cloud->add_static("b", 10);
+$cloud->add("c", "c.html", 10);
+
+$html = $cloud->html();
+is($html, q{<div id="htmltagcloud">
+<span class="tagcloud3even"><a href="a.html">a</a></span>
+<span class="tagcloud3odd">b</span>
+<span class="tagcloud3even"><a href="c.html">c</a></span>
+</div>});
+
 sub tags {
   return {
     'laptop'                 => 11,